<script lang="ts" setup>
import { ref, onMounted } from 'vue';
import api from '/@/api/email/index';
import { LoadingSvg } from '/@/utils/loading';
import Claim from './components/Claim.vue'

defineOptions({ name: 'VoucherIndex' })

const tableData = ref([]);
const tableHeight = ref(500);
const queryForm = ref<any>({
  state: '0',
  time: [],
  detailsTime: [],
});

const loading = ref(false);

const showClaim = ref(false)
const ClaimRef = ref()
const pagination = ref({
  page: 1,
  size: 10,
  total: 0
});

const handleQuery = () => {
  pagination.value.page = 1;
  getList();
};

const handleReset = () => {
  queryForm.value = {
    state: '0',
    time: [],
    detailsTime: [],
  };
};

const getList = async () => {
  loading.value = true;
  const res = await api.voucher.getList({
    pageNum: pagination.value.page,
    pageSize: pagination.value.size,
    ...queryForm.value
  });
  loading.value = false;
  if (res.code === 200) {
    tableData.value = res.data;
    pagination.value.total = res.total;
  }
};

const claim = (row: any) => {
  showClaim.value = true
  ClaimRef.value.getInfo(row.id)
}

const cancel = (row: any) => {
  ElMessageBox.confirm(`确认取消领取吗?`, '提示', { cancelButtonText: '取消', confirmButtonText: '确认' }).then(async () => {
    const res = await api.voucher.cancel(row.id);
    if (res.code === 200) {
      ElMessage({ type: 'success', message: '操作成功' });
      getList();
    }
  });
}

const handleCurrentChange = (page: number) => {
  pagination.value.page = page;
  getList();
};

onMounted(() => {
  getList();

  const setTableHeight = () => {
    tableHeight.value = window.innerHeight - 170 // 80 为顶部距离，根据实际情况设置
  }
  setTableHeight()
  window.addEventListener('resize', setTableHeight);
});
</script>
<template>
  <div class="main">
    <el-form class="btn_con" v-model="queryForm">
      <el-row :gutter="8">
        <el-col :span="3">
          <el-input-number v-model="queryForm.usdAmount" placeholder="总金额[美元]" :controls="false" class="w-100" />
        </el-col>
        <el-col :span="3">
          <el-select v-model="queryForm.type" placeholder="付款方式" clearable class="w-100">
            <el-option label="支票" :value="1"></el-option>
            <el-option label="TT" :value="2"></el-option>
            <el-option label="信用卡" :value="3"></el-option>
            <el-option label="ACH" :value="4"></el-option>
          </el-select>
        </el-col>
        <el-col :span="3">
          <el-input v-model="queryForm.pi" placeholder="PI" class="w-100" />
        </el-col>
        <el-col :span="3">
          <el-input-number v-model="queryForm.piUsdAmount" placeholder="认领金额" :controls="false" class="w-100" />
        </el-col>
        <el-col :span="5">
          <el-date-picker v-model="queryForm.time" type="daterange" range-separator="至" start-placeholder="凭证开始日期" end-placeholder="凭证结束日期" value-format="YYYY-MM-DD" style="width: 100%;" />
        </el-col>
        <el-col :span="5">
          <el-date-picker v-model="queryForm.detailsTime" type="daterange" range-separator="至" start-placeholder="认领开始日期" end-placeholder="认领结束日期" value-format="YYYY-MM-DD" style="width: 100%;" />
        </el-col>
        <el-col :span="2" class="add_btn">
          <el-button @click="handleQuery" type="primary">搜索</el-button>
          <el-button @click="handleReset" type="primary">重置</el-button>
        </el-col>
      </el-row>
    </el-form>
    <div>
      <el-tabs v-model="queryForm.state" @tab-change="handleQuery">
        <el-tab-pane label="待认领" name="0" />
        <el-tab-pane label="我的认领" name="1" />
      </el-tabs>
      <el-table :data="tableData" border style="width: 100%" :height="tableHeight" v-loading="loading"
        element-loading-svg-view-box="0, 0, 1024, 1024" :element-loading-svg="LoadingSvg">
        <el-table-column prop="voucherUrl" label="付款截图">
          <template #default="{ row }">
            <el-space v-if="row.voucherUrl">
              <el-image v-for="(item, index) in row.voucherUrl.split(',')" :key="index" :src="item" fit="cover"
                :zoom-rate="1.2" :max-scale="7" :min-scale="0.2" :initial-index="index"
                :preview-src-list="row.voucherUrl.split(',')" preview-teleported style="width: 110px; height: 60px;" />
            </el-space>
          </template>
        </el-table-column>
        <el-table-column prop="usdAmount" label="总金额[美元]" />
        <el-table-column prop="canUsdAmount" label="待认领金额[美元]" />
        <el-table-column prop="typeDisplay" label="付款方式" />
        <el-table-column prop="piDisplay" label="关联PI" />
        <el-table-column prop="voucherDate" label="凭证日期" />
        <el-table-column label="操作" width="100" fixed="right">
          <template #default="{ row }">
            <el-button v-if="+row.state === 0" plain type="primary" size="small" @click="claim(row)">认领</el-button>
            <el-button v-if="+row.state === 1" plain type="primary" size="small" @click="cancel(row)">取消认领</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination_con">
        <el-pagination @current-change="handleCurrentChange" :current-page="pagination.page" :total="pagination.total"
          small background layout="prev, pager, next" />
      </div>
    </div>

    <Claim ref="ClaimRef" v-model="showClaim" @refresh="getList" />
  </div>
</template>
<style lang="scss" scoped>
.main {
  width: 100%;
  height: 100vh;
  padding: 20px;
}

.pagination_con {
  display: flex;
  justify-content: end;
  padding: 10px;
  background: #fff;
}

.btn_con {
  padding: 7px 10px;
}

.add_btn {
  display: flex;
  justify-content: right;
  align-items: center;
}
.w-100 {
  width: 100%;
}
</style>